//
//  WBPictrueView.swift
//  WeiBo
//
//  Created by chenguanglei on 16/12/1.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SDWebImage

private let WBPictrueViewIdentifier =  "WBPictrueViewIdentifier"

// 定义配图距离cell的边距
private let WBPictureViewMargin: CGFloat = 10
// 定义配图的item的间距
private let WBPictureItemMargin: CGFloat = 5
// 配图的item的宽和高 : 宽和高一定要计算成一个整数
private let WBPictureItemWH: CGFloat = (kUIScreenWidth - 2*WBPictureViewMargin - 2*WBPictureItemMargin) / 3


class WBPictrueView: UICollectionView,UICollectionViewDataSource {

    var pic_urls: [WBPictureModel]?{
    
        didSet{

                let size = pictureSize()
              
                self.snp_updateConstraints(closure: { (make) in
                    
                    make.width.equalTo(size.width)
                    make.height.equalTo(size.height)
                    
                    
                })

                //刷新数据源
                reloadData()
          
        }
    
    }
   
    
    private func pictureSize() -> CGSize {
        
        guard let count = pic_urls?.count else {
            return CGSize.zero
        }
        
        if count == 1 {
            
            
            // 1.1 获取url -- urlString肯定有值
            let urlString = pic_urls?.first?.thumbnail_pic
            
            
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: urlString)
            
            guard var size = image?.size else {
                return CGSize.zero
            }
            
            
            if size.width < 150 {
                
                let w : CGFloat = 150
                let h = size.height * w / size.width
                size = CGSize(width: w, height: h )
                
            }else if size.width > 200{
                
                let w: CGFloat = 200
                let h = size.height * w / size.width
                size = CGSize(width: w, height: h)
            
            }
            
            // 单张图片的显示,比较复杂,复杂的东西往后放,怎么简单怎么来
            
            flowLayout.itemSize = size
            
            return size
        }
        
        if count == 4 {
            
            let four = WBPictureItemMargin + WBPictureItemWH*2
            
            flowLayout.itemSize = CGSize.init(width: WBPictureItemWH, height: WBPictureItemWH)
            
            return CGSize.init(width: four, height: four)
            
        }

        
        let row = (count - 1)/3 + 1
        
        flowLayout.itemSize = CGSize.init(width: WBPictureItemWH, height: WBPictureItemWH)
        
        // 处理普通情况
        return CGSize.init(width: (kUIScreenWidth - 2*WBPictureViewMargin), height: CGFloat((WBPictureItemWH + WBPictureItemMargin)*CGFloat(row)))
        
    }
    
    
    var flowLayout = UICollectionViewFlowLayout()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    func setupUI(){
    
        
        backgroundColor = UIColor(white: 240 / 255, alpha: 1)
        
        self.dataSource = self
        
        self.isScrollEnabled = false
        
        register(WBPictureViewCell.self, forCellWithReuseIdentifier: WBPictrueViewIdentifier)
        
        flowLayout.itemSize = CGSize.init(width: WBPictureItemWH, height: WBPictureItemWH)
        
        flowLayout.minimumLineSpacing = WBPictureItemMargin
        flowLayout.minimumInteritemSpacing = WBPictureItemMargin
    
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pic_urls?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WBPictrueViewIdentifier, for: indexPath) as! WBPictureViewCell
        
        cell.model = pic_urls![indexPath.item]
        
        
        return cell
    }
   

}



//自定义cell
class WBPictureViewCell: UICollectionViewCell {
    
    var model: WBPictureModel?{
    
        didSet{
    
            guard let urlString = model?.thumbnail_pic else {
                
                return
            }
            
            let url = URL(string: urlString)
            
            iconView.sd_setImage(with: url)
          
        }
    
    }

    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    
    
    private func setupUI(){
    
        contentView.addSubview(iconView)
        
        contentView.backgroundColor = UIColor(white: 240 / 255, alpha: 1)
    
        iconView .snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
    
    }
    
    
    private lazy var iconView:UIImageView = {
    
        let image = UIImageView()
    
        image.contentMode = .scaleToFill
        
        return image
    }()
    
    
    
}


